<template>
  <div>
    <el-aside class="el-aside" :width="isCollapse ? '64px' : '200px'">
      <el-row>
        <div class="menu-toggle-button" @click="collapseMenu">
          <i :class="isCollapse ? 'el-icon-caret-right' : 'el-icon-caret-left'"></i>
        </div>
        <!--菜单-->
        <el-menu
                background-color="#333744"
                text-color="#fff"
                active-text-color="#ffd04b"
                class="border-right-none"
                unique-opened
                :collapse-transition="false"
                :collapse="isCollapse"
                :router="true" :default-active="activeLink">
          <!--父级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuLIst" :key="item.id">
            <!--按钮-->
            <template slot="title">
              <!--图表-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--子菜单-->
            <el-menu-item-group>
              <!--子菜单项-->
              <el-menu-item v-for="sitem in item.children" :index="'/' + sitem.path" @click="saveLink('/' + sitem.path)">
                <template slot="title">
                  <!--图表-->
                  <i class="el-icon-menu"></i>
                  <!--文本-->
                  <span>{{sitem.authName}}</span>
                </template>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>

        </el-menu>
      </el-row>
    </el-aside>
  </div>
</template>

<script>
  export default {
    name: "HomeAside",
    data() {
      return {
        menuLIst: [],
        iconsObj: {
          '125': 'el-icon-user',
          '103': 'el-icon-s-platform',
          '101': 'el-icon-s-goods',
          '102': 'el-icon-loading' ,
          '145': 'el-icon-picture-outline'

        },
        isCollapse: false,
        widthMenu: 200,
        // 被激活的链接
        activeLink: ''
      }
    },
    methods: {
      getMenuLIst() {
        this.$http.get("menus").then(result => {
          // 状态码不等于200打印错误信息
          if (result.data.meta.status !== 200) return this.$message.error(result.data.meta.msg);
          this.menuLIst = result.data.data
        })
      },
      collapseMenu() {
        this.isCollapse = ! this.isCollapse
      },
      saveLink(link) {
        window.sessionStorage.setItem('menuActive',link)
      }
    },
    created (){
      this.getMenuLIst();
      this.activeLink = window.sessionStorage.getItem('menuActive');
    }
  }
</script>

<style scoped>
  .el-aside {
    background-color: #333744;
    height: 100%;

  }

  .menu-toggle-button {
    height: 40px;
    line-height: 40px;
    font-size: 20px;
    text-align: center;
    cursor: pointer;
    color: white;
  }
  .el-aside {
    transition: 0.5s;
  }
</style>